package com.uliky.shouyiLibrary.product.service.impl;

import com.uliky.shouyiLibrary.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uliky.common.utils.PageUtils;
import com.uliky.common.utils.Query;

import com.uliky.shouyiLibrary.product.dao.BrandDao;
import com.uliky.shouyiLibrary.product.entity.BrandEntity;
import com.uliky.shouyiLibrary.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.kerberos.KerberosKey;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //默认查询所有,添加模糊查询
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)) {
            wrapper.eq("brand_id",key).or().like("name", key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),wrapper

        );

        return new PageUtils(page);
    }

    /**
     * 详细更新.更新自己表以及关联表
     * @param brand
     */
    @Override
    @Transactional
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段的一致性
        //更新自己的表
        this.updateById(brand);
        //更新关联表
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //TODO:更新其他关联
        }
    }

}